package com.example.vae.Dao.PersonData;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.vae.Pojo.Person;

import java.util.List;


//个人信息类的工厂
public class PersonReposity {

    private static MutableLiveData<List<Person>> listLiveData;
    private static MutableLiveData<Person> personLiveData;
    private PersonDao personDao;

    private static MyHandle myHandle;
    private PersonListener personListener;


    private static Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            List<Person> people=(List<Person>)msg.obj;
            if(people.size()>0){
                listLiveData.setValue(people);
                personLiveData.setValue(listLiveData.getValue().get(0));
            }
        }
    };


    public PersonReposity(Context context) {
        myHandle=new MyHandle();
        PersonDatabase database = PersonDatabase.getDatabase(context.getApplicationContext());
        personDao = database.getpersonDao();
//        初始化信息
        listLiveData=new MutableLiveData<>();
        personLiveData = new MutableLiveData<>();
//        listLiveData.setValue(personDao.getAll());
//        personLiveData.setValue(listLiveData.getValue().get(0));
//        Log.d("PersonReposity 主页中",listLiveData.getValue().get(0).toString());
        new FindAllAsync(personDao).execute();

    }

    public MutableLiveData<Person> getPersonLiveData() {
        return personLiveData;
    }

    //    这里是 取得数据的地方
    public LiveData<List<Person>> getListLiveData() {
        new FindAllAsync(personDao).execute();
        return listLiveData;
    }

    public void InsertPerson(Person... people) {
        System.out.println("执行了插入用户的数据");
        new InsertAsynTask(personDao).execute(people);
    }

    //    用来更新数据
    public void updatePerson(Person... people) {
        System.out.println("执行了修改用户信息的 数据");
        new UpdateAsynTask(personDao).execute(people);
    }

//    外部用来查找信息的
    public void findAllPerson(){
        new Find_Send(personDao).execute();
    }

    //查找本地安卓是否由此用户
    public void findPersonByID(final Person person) {
        Log.d("findPersonByID   ", person.toString());

       List<Person> tem = listLiveData.getValue();

        if (tem==null) {
            InsertPerson(person);
        } else {
            updatePerson(person);
        }
        new FindAllAsync(personDao).execute();
        personLiveData.setValue(person);
        Log.d("findPersonByID", "进行了  " +listLiveData.getValue());
    }




    //    这个是插入的类
    static class InsertAsynTask extends AsyncTask<Person, Void, Void> {

        private PersonDao personDao;

        public InsertAsynTask(PersonDao personDao) {

            this.personDao = personDao;

        }

        @Override
        protected Void doInBackground(Person... people) {
          Log.d("InsertAsynTask 中",people[0].toString());
          personDao.insertPerson(people);

            return null;
        }
    }

    //    这个是更新的类
    static class UpdateAsynTask extends AsyncTask<Person, Void, Void> {

        private PersonDao personDao;

        public UpdateAsynTask(PersonDao personDao) {
            this.personDao = personDao;
        }

        @Override
        protected Void doInBackground(Person... people) {
            personDao.updatePerson(people);
            return null;
        }
    }

    //    这个是插入的类
    static class FindAllAsync extends AsyncTask<Person, Void, Void> {
        private PersonDao personDao;
        public FindAllAsync(PersonDao personDao) {
            this.personDao = personDao;
        }

        @Override
        protected Void doInBackground(Person... people) {
            Message message = new Message();
            message.obj = personDao.getAll();
            Log.d("FindAllAsync   ","所有信息"+personDao.getAll());
            handler.sendMessage(message);
            return null;
        }

    }


//    供外部使用的类



    //    这个是插入的类
    static class Find_Send extends AsyncTask<Person, Void, Void> {

        private PersonDao personDao;

        public Find_Send(PersonDao personDao) {
            this.personDao = personDao;
        }

        @Override
        protected Void doInBackground(Person... people) {
            Message message = new Message();
            message.obj = personDao.getAll();
           Log.d("Find_Send","所有信息"+personDao.getAll());
            myHandle.sendMessage(message);
            return null;
        }

    }

    //    定义一个消息，发送给外部
    class   MyHandle extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            personListener.success((List<Person>)msg.obj);
        }
    }

    public void setPersonListener(PersonListener personListener){
        this.personListener=personListener;
    }
    public interface PersonListener{
        void success(List<Person> people);
    }


}
